/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Filter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;

/**
 *
 * @author Gjun
 */
public class UploadServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UploadServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet UploadServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        response.sendRedirect("ProductInsert.jsp");
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        String x="";
        try {
            //processRequest(request, response);                   
            DiskFileUpload up=new DiskFileUpload();
            List<FileItem> list =up.parseRequest(request);
            for (FileItem item:list){
                if(item.isFormField()){
                    x+="'"+new String(item.getString().getBytes("ISO8859_1")
                            ,"UTF-8")+"',";
                    //System.out.println(x);
                }
                else{
                    InputStream in = item.getInputStream();
                    String path="C:\\JavaSe\\project\\GGteam_web_devolope\\trunk\\trunk\\Web_front_end\\web\\upload\\"+item.getName();
                    FileOutputStream fout = new FileOutputStream(path);
                    byte ary[]=new byte[1024];
                    int len=0;
                    while((len=in.read(ary))>-1){
                        fout.write(ary, 0, len);
                    }
                    in.close();
                    fout.close();
                }
            }
        } catch (FileUploadException ex) {
            request.setAttribute("errmes", "新增失敗");
            request.getRequestDispatcher("ProductInsert.jsp").forward(request, response);
            Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);            
        }
        String sql="INSERT INTO product VALUES("+x+"null)";
        //System.out.println(sql);
        DAO d=new DAO();
        try{
            d.modify(sql,new String[]{});
        }
        catch(Exception e){
            request.setAttribute("errmes", "新增失敗");
            request.getRequestDispatcher("ProductInsert.jsp").forward(request, response);
        }                   
        request.getRequestDispatcher("ProductManagement.jsp").forward(request, response);
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    boolean checkEmpty(String str){
        boolean f1 =false;
        if(str==null)
        {
            f1=true;
            return f1;
        }
        else if(str.trim().equals(""))
        {
            f1=true;
            return f1;
        }
        return f1;
    }
}
